﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FizzBuzz.Service;
using Autofac.Features.Metadata;

namespace FizzBuzz.ServiceImpl
{
    public class FizzBuzzRuleEngine:IRuleEngine
    {
        public IEnumerable<Meta<IRule, IRuleMetadata>> Rules { get; set; }

        public string ProcessRules(int input)
        {
            var result = input.ToString();
            if (Rules.Any(x=>x.Value.RuleMatch(input)))
            {
                var sortedRules = Rules.OrderBy(x=>x.Metadata.RuleOrder).Select(x=>x.Value);
                result = string.Empty;
                foreach (IRule rule in sortedRules)
                {
                    result += rule.ProcessInput(input);
                }
            }
            
            return result;
        }
    }
}
